#!/bin/bash
#zhushi:Linux应用运行分析
#Author_Name=Weiyi

Resettem=$(tput sgr0)
read -p "Please Input Url:" Nginxserver

#mysql user/password

Mysqlserver="10.156.11.233"
Mysqlu="user"
Mysqlp="imooc"
Mysqlport="3306"

Check_Nginx_server(){
echo -e '\E[35m]' "############################# Nginx server Information (Nginx服务信息) ##########################################\n"

	status_code=$(curl -m 5 -s -w %{http_code} $Nginxserver -o /dev/null)
	# "m":最大的传输时间,s:静音模式,w:显示出Http_Code,o:输出/dev/null
	if [ $status_code -eq 000 -o $status_code -ge 500 ];then
		echo -e '\E[30m]' "Check http server error! Response:" $Resettem $status_code
	else
		http_content=$(curl -s $Nginxserver)
		echo -e '\E[32m]' "Check http server ok!:" $Resettem $http_content
	fi
	
}

Check_Mysql_server(){
echo -e '\E[35m]' "############################# Mysql server Information (Mysql主从服务信息) ##########################################\n"
	
# "z":检测端口的,"w2":超时时间,成功输出echo $? 为1则成功，而为1则失败
	nc -z -w2 ${Mysqlserver} ${Mysqlport} &>/dev/null
	if [ $? -eq 0  ];then
		echo -e '\E[32m' "Connect ${Mysqlserver}:${Mysqlport} Successful\n" $Resettem
		mysql -u ${Mysqlu} -p ${Mysqlp} -h ${Mysqlserver} -e "Show slave status\G" |grep "Slave_IO_Running" |awk '{if( $2 != "Yes"){print "Slave Thread Not Running!";exit 1}}'
		if [ $? -eq 0 ];then
			mysql -u ${Mysqlu} -p ${Mysqlp} -h ${Mysqlserver} -e "Show slave status\G" |grep "Seconds_Behind_Master"
		else
			echo -e '\E[30m' "Connect Mysql server not succeeded" $Resettem
		fi
	fi
}

#Check Nginx server Information
Check_Nginx_server

#Check Mysql server Information
#Check_Mysql_server

#主从安全监控权限与用户
#mysql>grant replication slave,replication client on *.* to user@'%' indentified by 'password';
